<?php

class NewsIndexController extends IndexController 
{
    public function prepare()
    {
        $filter = array();
        
        $not_in_list = null;
        if (Env::getParam('id') || Env::getParam('words_url')) {
            $news_main = new NewsMainController();
            $news_main->prepare();
            
            $this->view->assign('main_news', $news_main->display());
            
            $not_in_list = $news_main->getNews()->getId();
        } elseif (Env::getParam('rss_id')) {
            $rss_model = new RssModel();
            
            $rss = $rss_model->get(Env::getParam('rss_id'));
            
            if (!$rss) {
                Location::reload('/');
            }
            
            define('PAGE_TITLE', ' | новости от '.$rss->getUrl());
            define('META_DESC', 'Футбольные новости от '.$rss->getUrl());
            
            $filter['rss_id'] = $rss->getId();
        }
        
        $page = Env::request('page', 1);
        
        if (!defined('PAGE_TITLE') && $page > 1) {
            define('PAGE_TITLE', ' | Лента спортивных новостей | Страница '.$page);
            define('META_DESC', 'Лента футбольных новостей, страница '.$page);
        }
        
        $order_field = Env::get('order_field', 'pubdate');
        $order_by = Env::get('order_by', 'DESC');

        $order = array(
            'field' => $order_field,
            'by' => $order_by
        );
        
        $NewsModel = new NewsModel();
        $news = $NewsModel->getList($page, 20, News::STATUS_ACTIVE, $filter, $order, $not_in_list);

        $Pager = new Pager($NewsModel->getTotalCount(), $page, 20);
//        var_dump($news); die();
        $this->view->assign('pager', $Pager);
        $this->view->assign('list', $news);
    }
}